﻿CREATE FUNCTION [dbo].[tf_system_SplitString]
(
	@List NVARCHAR(MAX),
	@Delimiter NVARCHAR(5),
	@EmptyStringYeildsNULL BIT = 0
)
RETURNS @RtnValue TABLE 
(
	RowID int IDENTITY(1,1), 
	RowValue NVARCHAR(256)
)
AS
BEGIN
	DECLARE @Value NVARCHAR(256)
	WHILE (Charindex(@Delimiter,@List)>0)
	BEGIN

			SET @Value = ltrim(rtrim(Substring(@List,1,Charindex(@Delimiter,@List)-1)))
			Insert Into @RtnValue (Rowvalue)
			Select CASE WHEN LEN(@Value) = 0 AND @EmptyStringYeildsNULL = 1 
					THEN NULL 
					ELSE @Value 
					END 

			Set @List = Substring(@List,Charindex(@Delimiter,@List)+len(@Delimiter),len(@List))
		END

		SET @Value = ltrim(rtrim(@List))
		Insert Into @RtnValue (Rowvalue)
		Select CASE WHEN LEN(@Value) = 0 AND @EmptyStringYeildsNULL = 1 
					THEN NULL 
					ELSE @Value 
					END 
	
		RETURN
END